<template>
<div class="user">
    <div class="common-seach-wrap">
      <el-form
        size="small"
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
      >
        <el-form-item :label="$t('plus.commission_settlement')">
          <el-select v-model="formInline.isSettled" :placeholder="$t('plus.commission_to_settle')">
            <el-option :label="$t('plus.all')" value="-1"></el-option>
            <el-option :label="$t('plus.commission_settled')" value="1"></el-option>
            <el-option :label="$t('plus.commission_unsettled')" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('plus.user_id_lower')"
          ><el-input
            v-model="formInline.userId"
            :placeholder="$t('plus.input_user_id')"
          ></el-input
        ></el-form-item>
        <el-form-item
          ><el-button type="primary" @click="onSubmit"
            >{{$t('plus.search')}}</el-button
          ></el-form-item
        >
        <el-form-item
          ><el-button
            size="small"
            type="success"
            @click="onExport"
            v-auth="'/plus/agent/order/export'"
            >{{$t('plus.export')}}</el-button
          ></el-form-item
        >
      </el-form>
    </div>

    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table
          :data="tableData"
          size="small"
          border
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column
            prop="orderMaster.orderNo"
            :label="$t('plus.order_number')"
            width="155"
          ></el-table-column>
          <el-table-column
            prop="order_master.create_time"
            :label="$t('plus.product_info')"
            show-overflow-tooltip
            width="300"
          >
            <template #default="scope">
              <div
                class="product-info p-10-0"
                v-for="(item, index) in scope.row.orderMaster.productList"
                :key="index"
              >
                <div class="pic"><img v-img-url="item.imagePath" alt="" /></div>
                <div class="info">
                  <div class="name gray3">{{item.productName}}</div>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="referee.value" :label="$t('plus.distributor1')" width="400">
            <template #default="scope">
              <div class="d-s-s d-c">
                <div
                  class="d-s-c ww100 border-b-d"
                  v-if="scope.row.firstUserId > 0"
                >
                  <p class="referee-name text-ellipsis">
                    <span class="gray9">{{$t('plus.first_level_distributor')}}</span>
                    <span class="blue">{{scope.row.agentFirst.nickName}}

</span>
                  </p>
                  <p class="referee-name text-ellipsis">
                    <span class="gray9">{{$t('plus.user_id_colon')}}</span>
                    <span class="gray6">{{scope.row.agentFirst.userId}}</span>
                  </p>
                  <p class="referee-name text-ellipsis">
                    <span class="gray9">{{$t('plus.commission_amount')}}</span>
                    <span class="orange">￥{{scope.row.firstMoney}}</span>
                  </p>
                </div>
                <div
                  class="d-s-c ww100 border-b-d"
                  v-if="scope.row.secondUserId > 0"
                >
                  <p class="referee-name text-ellipsis">
                    <span class="gray9">{{$t('plus.second_level_distributor')}}</span>
                    <span class="blue">{{scope.row.agentSecond.nickName}}

</span>
                  </p>
                  <p class="referee-name text-ellipsis">
                    <span class="gray9">{{$t('plus.user_id_colon')}}</span>
                    <span class="gray6">{{scope.row.agentSecond.userId}}

</span>
                  </p>
                  <p class="referee-name text-ellipsis">
                    <span class="gray9">{{$t('plus.commission_amount')}}</span>
                    <span class="orange">￥{{scope.row.secondMoney}}</span>
                  </p>
                </div>
                <div
                  class="d-s-c ww100 border-b-d"
                  v-if="scope.row.thirdUserId > 0"
                >
                  <p class="referee-name text-ellipsis">
                    <span class="gray9">{{$t('plus.third_level_distributor')}}</span>
                    <span class="blue">{{scope.row.agentThird.nickName}}

</span>
                  </p>
                  <p class="referee-name text-ellipsis">
                    <span class="gray9">{{$t('plus.user_id_colon')}}</span>
                    <span class="gray6">{{scope.row.agentThird.userId}}</span>
                  </p>
                  <p class="referee-name text-ellipsis">
                    <span class="gray9">{{$t('plus.commission_amount')}}</span>
                    <span class="orange">￥{{scope.row.thirdMoney}}</span>
                  </p>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="nickName" :label="$t('plus.unit_price_quantity')">
            <template #default="scope">
              <div
                v-for="(item, index) in scope.row.orderMaster.productList"
                :key="index"
              >
                <span class="orange">￥{{item.productPrice}}</span> ×{{item.totalNum}}

              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="orderMaster.payPrice"
            :label="$t('plus.actual_payment')"
            width="100"
          >
            <template #default="scope">
              <span class="fb orange">{{scope.row.orderMaster.payPrice}}

</span>
            </template>
          </el-table-column>
          <el-table-column prop="nickName" :label="$t('plus.buyer')"></el-table-column>
          <el-table-column prop="mobile" :label="$t('plus.transaction_status')" width="120">
            <template #default="scope">
              <p>
                <span class="gray9">{{$t('plus.payment_status1')}}</span> {{scope.row.orderMaster.payStatusText}}

              </p>
              <p>
                <span class="gray9">{{$t('plus.shipping_status')}}</span> {{scope.row.orderMaster.deliveryStatusText}}

              </p>
              <p>
                <span class="gray9">{{$t('plus.receipt_status')}}</span> {{scope.row.orderMaster.receiptStatusText}}

              </p>
            </template>
          </el-table-column>
          <el-table-column prop="referee.value" :label="$t('plus.commission_settlement')">
            <template #default="scope">
              <span class="green" v-if="scope.row.isSettled == 1">{{$t('plus.commission_settled')}}</span>
              <span class="red" v-if="scope.row.isSettled == 0">{{$t('plus.commission_unsettled')}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="orderMaster.createTime"
            :label="$t('plus.order_time')"
            width="130"
          ></el-table-column>
        </el-table>
      </div>

      <!--分页-->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="curPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import PlusApi from "@/api/plus.js";
import qs from "qs";
import { useUserStore } from "@/store";
const {
  token
} = useUserStore();
export default {
  components: {
    /*编辑组件*/
  },
  data() {
    return {
      token,
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 15,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      formInline: {
        isSettled: "-1",
        /*用户ID*/
        userId: ""
      },
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*当前编辑的对象*/
      userModel: {}
    };
  },
  props: {},
  watch: {
    $route(to, from) {
      if (to.query.userId != null) {
        this.formInline.userId = to.query.userId;
      } else {
        this.formInline.userId = "";
      }
      this.curPage = 1;
      this.getData();
    }
  },
  created() {
    if (this.$route.query.userId != null) {
      this.formInline.userId = this.$route.query.userId;
    }
    /*获取列表*/
    this.getData();
  },
  methods: {
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getData();
    },
    /*获取数据*/
    getData(userId) {
      let self = this;
      let Params = {
        userId: self.formInline.userId,
        pageIndex: self.curPage,
        pageSize: self.pageSize,
        isSettled: self.isSettled
      };
      PlusApi.agentOrder(Params, true).then(res => {
        self.loading = false;
        self.tableData = res.data.records;
        self.totalDataNumber = res.data.total;
      }).catch(error => {
        self.loading = false;
      });
    },
    //搜索
    onSubmit() {
      let self = this;
      self.loading = true;
      self.isSettled = self.formInline.isSettled;
      self.getData();
    },
    onExport: function () {
      let self = this;
      let Params = {
        userId: self.formInline.userId,
        pageIndex: self.curPage,
        pageSize: self.pageSize,
        isSettled: self.isSettled
      };
      Params.tokenshop = self.token;
      let baseUrl = import.meta.env.VITE_BASIC_URL;
      if (!baseUrl) {
        baseUrl = window.location.protocol + "//" + window.location.host;
      }
      window.location.href = baseUrl + "/api/shop/plus/agent/order/export?" + qs.stringify(Params);
    },
    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getData();
    },
    /*打开弹出层编辑*/
    editClick(item) {
      this.userModel = item;
      this.open_edit = true;
    },
    /*关闭弹窗*/
    closeDialogFunc(e, f) {
      if (f == "add") {
        this.open_add = e.openDialog;
        if (e.type == "success") {
          this.getData();
        }
      }
      if (f == "edit") {
        this.open_edit = e.openDialog;
        if (e.type == "success") {
          this.getData();
        }
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.referee-name {
  width: 33.333333%;
}
</style>

